﻿using CommandProcessor.Dispatcher;
using Data.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using AutoMapper;
using QuanLySanXuat.Models;
using Domain.Commands;
using Core.Common;
using Core.Extensions;
using Data.Models;
namespace QuanLySanXuat.Controllers
{
    public class ActualGoodInStockController : Controller
    {
        private readonly ICommandBus commandBus;
        private readonly IActualGoodInStockRepository ActualGoodInStockRepository;
        private readonly ICompanyRepository companyRepository;
        private readonly IStockRepository stockRepository;
        private readonly IUOMRepository uomRepository;
        private readonly IProductRepository goodRepository;
        public ActualGoodInStockController(ICommandBus commandBus, IActualGoodInStockRepository ActualGoodInStockRepository, ICompanyRepository companyRepository, IStockRepository stockRepository, IUOMRepository uomRepository,IProductRepository goodRepository)
        {
            ModelState.Clear();
            this.commandBus = commandBus;
            this.ActualGoodInStockRepository = ActualGoodInStockRepository;
            this.companyRepository = companyRepository;
            this.stockRepository = stockRepository;
            this.uomRepository = uomRepository;
            this.goodRepository = goodRepository;
        }
        //
        // GET: /ActualGoodInStock/
        [HttpGet]
        public ActionResult Index()
        {
            var prodcutType = ActualGoodInStockRepository.GetAll();
            ViewData["UOM"] = uomRepository.GetAll();
            ViewData["Company"] = companyRepository.GetAll();
            ViewData["Stock"] = stockRepository.GetAll();
            ViewData["Good"] = goodRepository.GetAll();
            return View(prodcutType.ToPagedList(1, 10));
        }
        [HttpGet]
        public ActionResult Create()
        {
            ModelState.Clear();
            return View();
        }
        [HttpPost]
        public ActionResult Save(ActualGoodInStockFormModels frm)
        {
            var form = new ActualGoodInStockFormModels()
            {
                ID = frm.ID,
                CompanyID=frm.CompanyID,
                DateIn=DateTime.Now,
                GoodID=frm.GoodID,
                InputPrice=frm.InputPrice,
                InputQuantity=frm.InputQuantity,
                OutputQuantity=frm.OutputQuantity,
                RemainQuantity=frm.RemainQuantity,
                StockID=frm.StockID,
                UOMID=frm.UOMID,
            };
            var command = Mapper.Map<ActualGoodInStockFormModels, CreateOrUpdateActualGoodInStockCommand>(form);
            IEnumerable<ValidationResult> errors = commandBus.Validate(command);
            ModelState.AddModelErrors(errors);
            if (ModelState.IsValid)
            {
                var result = commandBus.Submit(command);

            }
            var model = ActualGoodInStockRepository.GetAll();
            return PartialView("_ActualGoodInStockList", model.ToPagedList(1, 10));
        }

        [HttpGet]
        public JsonResult Get(int ID)
        {
            ActualGoodInStock product = ActualGoodInStockRepository.GetById(ID);
            var obj = Mapper.Map<ActualGoodInStock, ActualGoodInStockFormModels>(product);
            return Json(obj, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult RemoveItems(string[] ProID)
        {
            List<DeleteActualGoodInStockCommand> list = new List<DeleteActualGoodInStockCommand>();
            foreach (var item in ProID)
            {
                var command = new DeleteActualGoodInStockCommand { ID = Convert.ToInt16(item) };
                list.Add(command);
            }
            var result = commandBus.SubmitRange(list);
            var model = ActualGoodInStockRepository.GetAll();
            return PartialView("_ActualGoodInStockList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        public ActionResult Search(string Name)
        {
            IEnumerable<ActualGoodInStock> model;
            if (String.IsNullOrEmpty(Name))
            {
                model = ActualGoodInStockRepository.GetAll();
            }
            else
            {
                model = ActualGoodInStockRepository.GetMany(x => (x.Stock.Name.Contains(Name)) || (x.Stock.Description.Contains(Name)));

            }
            return PartialView("_ActualGoodInStockList", model.ToPagedList(1, 10));
        }
        [HttpGet]
        [ValidateInput(false)]
        public ActionResult Page(int PageID)
        {
            var prodcutType = ActualGoodInStockRepository.GetAll();
            return PartialView("_ActualGoodInStockList", prodcutType.ToPagedList(1, PageID));
        }
    }
}
